package org.jwcloud.project.module.zx.base;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import org.jwcloud.project.module.zx.dao.BaseDAO;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseServiceImpl {
	
	
	// 查询阶段
	protected final String APPP_HASE_KEY = "01:立项,02:勘察设计,03:招投标,04:施工,05:验收,06:结算,07:交付,08:保修";

	@Autowired
	protected BaseDAO baseDAO;

	/**
	 * 校验日期是否是工作日
	 * 
	 * @param date
	 * @return
	 */
	protected boolean checkWorkingDate(Date theDate) {
		boolean isWorking = true;
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		Map<String,Object> theDateMap = baseDAO.searchWorkingDate(theDate);
		if (theDateMap != null) {
			// 1节假日 2休息转工作日 3正常休息日
			String state = (String) theDateMap.get("state");

			if ("1".equals(state) || "3".equals(state)) {
				isWorking = false;
			} else {
				isWorking = true;
			}

		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
				|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			// 周六 周日
			isWorking = false;
		}
		return isWorking;

	}

	/**
	 * 查询用户姓名
	 * 
	 * @param userid
	 * @return
	 */
	protected String getUserName(String userid) {
		String userName = "";
		if (userid != null) {
			Map map = baseDAO.searchUserInfo(userid);
			if (map != null) {
				userName = (String) map.get("real_name");
			}
		}
		return userName;
	}

	/**
	 * 查询部门名称
	 * 
	 * @param deptID
	 * @return
	 */
	protected String getDeptName(String deptID) {
		String deptName = "";
		if (deptID != null) {
			Map map = baseDAO.searchDeptInfo(deptID);
			if (map != null) {
				deptName = (String) map.get("title");
			}
		}
		return deptName;
	}
	/**
	 * 计算指定日期后端工作日
	 * @param theDate
	 * @param durTime
	 * @return
	 */
	protected Date nextWorkDate(Date theDate ,Integer durTime ) {
		int j = 1;
		while (j <= durTime) {

			Calendar calendar = new GregorianCalendar();
			calendar.setTime(theDate);
			calendar.add(calendar.DATE, 1);
			theDate = calendar.getTime();
			if (checkWorkingDate(theDate)) {
				// 如果不是工作日
				j++;
			}
		

		}
		return theDate;
	}
	
	
	public Date getDBDate() {
//		Timestamp d = new Timestamp(System.currentTimeMillis());
//		
//		return new Date(d.getTime());
		return baseDAO.searchDBDate();
	}
	
	
}
